package com.linko.intelligent.sys.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linko.intelligent.sys.core.mapper.InstitutionMapper;
import com.linko.intelligent.sys.core.service.InstitutionService;
import com.linko.intelligent.sys.core.service.UserService;
import com.linko.intelligent.sys.pojo.dto.InstitutionDto;
import com.linko.intelligent.sys.pojo.po.Institution;
import com.linko.intelligent.sys.pojo.query.InstitutionQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yuan
 * @create 2024/3/15 11:42
 */
@Service
public class InstitutionServiceImpl extends ServiceImpl<InstitutionMapper, Institution> implements InstitutionService {


    @Autowired
    private InstitutionMapper organizeMapper;

    @Autowired
    private UserService userService;

    @Override
    public Long save(InstitutionDto dto) {
        Institution po = new Institution();
        BeanUtils.copyProperties(dto, po);
        save(po);
        return po.getId();
    }

    @Override
    public Boolean delete(Long id) {
        Institution po = new Institution();
        po.setId(id);
        po.setDelFlag("1");
        return updateById(po);
    }

    @Override
    public Boolean update(Long id, InstitutionDto dto) {
        Institution po = new Institution();
        BeanUtils.copyProperties(dto, po);
        po.setId(id);
        return updateById(po);
    }

    @Override
    public Institution getDetail(Long id) {
        Institution po = getById(id);
        return po;
    }

    @Override
    public List<Institution> queryList(InstitutionQuery query) {
        List<Institution> list = organizeMapper.selectLs(query);
        return list;
    }


    @Override
    public PageInfo<Institution> queryPage(InstitutionQuery query) {
        PageInfo<Institution> pageInfo = PageHelper.startPage(query.getPn(), query.getPs()).doSelectPageInfo(
                () -> queryList(query)
        );
        return pageInfo;
    }

    @Override
    public List<Institution> getTreeById(InstitutionQuery query) {
        List<Institution> nodeList = organizeMapper.selectLs(query);
        Map<String, Institution> nodeMap = new HashMap<>();
        for (Institution node : nodeList) {
            nodeMap.put(node.getInstitutionCode(), node);
        }

        List<Institution> tree = new ArrayList<>();
        for (Institution node : nodeList) {
            String parentCode = node.getParentCode();
            if (parentCode != null && nodeMap.containsKey(parentCode)) {
                Institution parent = nodeMap.get(parentCode);
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<>());
                }
                parent.getChild().add(node);
            } else {
                tree.add(node);
            }
        }
        return tree;
    }

}
